package com.wcar.user.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.wcar.common.entity.*;
import com.wcar.common.util.WebUtil;
import com.wcar.common.vo.Result;
import com.wcar.user.dao.*;
import com.wcar.user.service.UserService;
import com.wcar.user.vo.UserVo;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.StringTokenizer;

/**
 * @program: four-wcar-idea
 * @description:
 * @author: 吴朝阳
 * @create: 2024-05-27 16:40
 **/
@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserDao userDao;
    @Autowired
    private RoleDao roleDao;
    @Autowired
    private FrontUserDao frontUserDao;
    @Autowired
    private UserAddressDao userAddressDao;
    @Autowired
    private StringRedisTemplate redisTemplate;
    @Autowired
    private LoginLogDao loginLogDao;
    @Override
    public Result<Page<User>> getUser(Integer currentPage, Integer pageSize, UserVo userVo) {
        Page<User> page=new Page<>(currentPage,pageSize);
        QueryWrapper<User> wrapper = new QueryWrapper<>();
        if(userVo.getUserName()!=null && userVo.getUserName()!=""){
            wrapper.like("user_name", userVo.getUserName());
        }
        if(userVo.getPhonenumber()!=null && userVo.getPhonenumber()!="") {
            wrapper.like("phonenumber", userVo.getPhonenumber());
        }
        Page<User> userPage = userDao.selectPage(page, wrapper);
        return new Result<>(2000,"查询用户成功",userPage);
    }
    @Override
    public Result<User> addUser(User user) {
        //获取token
        String token = WebUtil.getToken();
        ValueOperations<String, String> forValue = redisTemplate.opsForValue();
        String s = forValue.get("user:login:" + token);
        User currentuser = JSON.parseObject(s, User.class);
            user.setCreateBy(currentuser.getUserName());
            user.setCreateTime(new Date());
            Md5Hash md5Hash = new Md5Hash("at",null,5);
            user.setSalt(md5Hash.toString());
            Md5Hash md5Hash1 = new Md5Hash(user.getPassword(),user.getSalt(),5);
            user.setPassword(md5Hash1.toString());
            String addr = WebUtil.getRequest().getRemoteAddr();
            user.setLoginIp(addr);
            userDao.insert(user);

        return new Result<>(2000,"添加成功",user);
    }

    @Override
    public Result<User> updateUser(User user) {
        System.out.println(user+"~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~");
        userDao.updateById(user);
        return new Result<>(2000,"修改成功",user);
    }

    @Override
    public Result<User> deleteUser(User user) {
        userDao.deleteById(user);
        return new Result<>(2000,"删除成功",user);
    }

    @Override
    public Result<Integer> updateRole(Integer userId, List<Integer> roleIds) {

        userDao.deleteByUserId(userId);
        for (Integer roleId : roleIds) {
            userDao.updateRole(userId,roleId);
            return new Result<>(2000,"授权成功",null);
        }
        return new Result<>(5000,"授权失败",null);
    }

    @Override
    public Result<List<Role>> selectAllRole() {
        List<Role> roles = roleDao.selectList(null);
        return new Result<>(200,"查询所有角色成功",roles);
    }

    @Override
    public Result<Integer> addLoginLog(LoginLog user) {
        loginLogDao.insert(user);
        return null;
    }
}
